package com.kude.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kude.entity.ProductCategory;


/**
 * A data access object (DAO) providing persistence and search support for
 * ProductCategory entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see common.dao.ProductCategory
 * @author MyEclipse Persistence Tools
 */

public class ProductCategoryDAO extends BaseDAO<ProductCategory,Integer> {
	private static final Logger log = LoggerFactory
			.getLogger(ProductCategoryDAO.class);
	// property constants
	public static final String CATEGORY_NAME = ProductCategory.CATEGORY_NAME;
	public static final String PREFIX = ProductCategory.PREFIX;
	public static final String PARENTID = ProductCategory.PARENTID;
	public static final String PRODUCTS = ProductCategory.PRODUCTS;
	

	public ProductCategoryDAO() {
		super(ProductCategory.class);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ProductCategory instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProductCategory as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCategoryName(Object categoryName) {
		return findByProperty(CATEGORY_NAME, categoryName);
	}
	

	public List findAll() {
		log.debug("finding all ProductCategory instances");
		try {
			String queryString = "from ProductCategory";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ProductCategory merge(ProductCategory detachedInstance) {
		log.debug("merging ProductCategory instance");
		try {
			ProductCategory result = (ProductCategory) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ProductCategory instance) {
		log.debug("attaching dirty ProductCategory instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ProductCategory instance) {
		log.debug("attaching clean ProductCategory instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}